package com.yzs.exam.controller.admin;

import com.github.pagehelper.PageInfo;
import com.sun.java.swing.plaf.motif.MotifEditorPaneUI;
import com.yzs.exam.common.base.BaseApiController;
import com.yzs.exam.common.base.RestResponse;
import com.yzs.exam.entity.Subject;
import com.yzs.exam.service.SubjectService;
import com.yzs.exam.utils.PageInfoHelper;
import com.yzs.exam.viewmodel.admin.education.SubjectEditRequestVM;
import com.yzs.exam.viewmodel.admin.education.SubjectPageRequestVM;
import com.yzs.exam.viewmodel.admin.education.SubjectResponseVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author yzs
 * @create 2021-03-27 13:23
 */
@Api(value = "学科模块", tags = {"学科接口"})
@RestController("AdminEducationController")
@RequestMapping(value = "/api/admin/education")
@AllArgsConstructor
public class EducationController extends BaseApiController {

    private final SubjectService subjectService;


    @RequestMapping(value = "/subject/list",method = RequestMethod.POST)
    public RestResponse<List<Subject>> list(){
        List<Subject> subjects = subjectService.allSubject();
        return RestResponse.ok(subjects);
    }

    @RequestMapping(value = "/subject/page",method = RequestMethod.POST)
    public RestResponse<PageInfo<SubjectResponseVM>> pageList(@RequestBody SubjectPageRequestVM model){
        PageInfo<Subject> pageInfo = subjectService.page(model);
        PageInfo<SubjectResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> modelMapper.map(e, SubjectResponseVM.class));
        return RestResponse.ok(page);
    }

    @RequestMapping(value = "/subject/edit",method = RequestMethod.POST)
    public RestResponse edit(@RequestBody @Valid SubjectEditRequestVM model){
        Subject subject = modelMapper.map(model, Subject.class);
        if (model.getId() == null){//如果modelID为空 -- 添加 --插入
            subject.setDeleted(false);
            subjectService.insertByFilter(subject);
        }else {////如果modelID为空 -- 更新
            subjectService.updateByIdFilter(subject);
        }
        return RestResponse.ok();
    }

    @RequestMapping(value = "/subject/select/{id}",method = RequestMethod.POST)
    public RestResponse<SubjectEditRequestVM> select(@PathVariable Integer id){
        Subject subject = subjectService.selectById(id);
        SubjectEditRequestVM vm = modelMapper.map(subject, SubjectEditRequestVM.class);
        return RestResponse.ok(vm);
    }

    //删除用户 -- 将用户置为不可用
    @RequestMapping(value = "/subject/delete/{id}" ,method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id){
        Subject subject = subjectService.selectById(id);
        subject.setDeleted(true);
        subjectService.updateByIdFilter(subject);//字段null值的不更新，不为null的更新
        return RestResponse.ok();
    }
}
